//
//  BVMineProfessionalAlertView.m
//  XiangYu
//
//  Created by YYKJ on 2017/6/24.
//  Copyright © 2017年 YYKJ. All rights reserved.
//

#import "BVMineProfessionalAlertView.h"
#import "BVAllAlertTool.h"
#import "BVAddressModel.h"

#define AlertW [UIScreen mainScreen].bounds.size.width-80
@interface BVMineProfessionalAlertView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic ,retain) UIView *alertView;

@property (nonatomic ,strong) BVAllAlertTool   *mineTool;

@property (nonatomic ,strong) UIView           *upLine;
@property (nonatomic ,strong) UIButton         *upLeftBtn;
@property (nonatomic ,strong) UIButton         *upRightBtn;
@property (nonatomic ,strong) UIPickerView     *pickView;

@property (nonatomic ,strong) NSMutableArray   *days;
@property (nonatomic ,strong) NSMutableArray   *hours;
@property (nonatomic ,strong) NSMutableArray   *minutes;

@property (nonatomic ,strong) NSString         *tempStr;

@end

@implementation BVMineProfessionalAlertView
- (instancetype)init{
    
    if (self = [super init]) {
        
        [self setFrame:[UIScreen mainScreen].bounds];
        [self setBackgroundColor:[UIColor BVCoverColor]];
        
        self.alertView = [[UIView alloc]init];
        [self addSubview:self.alertView];
        [self.alertView setBackgroundColor:[UIColor whiteColor]];
        [self.alertView setFrame:CGRectMake(0, 0, AlertW, 200)];
        [self.alertView.layer setCornerRadius:5.0];
        [self.alertView.layer setPosition:self.center];
        
        self.upLeftBtn = [self upBtnWithTitle:@"取消" withTag:100];
        [self.upLeftBtn setFrame: CGRectMake(10, 10, 60, 25)];
        
        self.upRightBtn = [self upBtnWithTitle:@"确认" withTag:200];
        [self.upRightBtn setFrame:CGRectMake(AlertW-10-60, 10, 60, 25)];
        
        self.upLine = [[UIView alloc]initWithFrame:CGRectMake(0,40,AlertW,1.0)];
        [self.upLine setBackgroundColor:[UIColor BVLineGrayColor]];
        [self.alertView addSubview:self.upLine];
        
        self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 50, self.alertView.width, self.alertView.height-60)];
        [self.alertView addSubview:self.pickView];
        [self.pickView setDelegate:self];
        [self.pickView setDataSource:self];
        
        [self fetchProfessionalData];
    }
    return self;
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.days.count;
    }else if(component == 1){
        return self.hours.count;
    }else{
        return self.minutes.count;
    }
}
#pragma mark - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    if (component == 0) {
        BVAddressModel *province  = self.days[row];
        return province.name;
    } else if(component == 1){
        BVAddressModel *model = self.hours[row];
        return model.name;
    }else{
        BVAddressModel *model = self.minutes[row];
        return model.name;
    }
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setFont:[UIFont systemFontOfSize:20]];
        [pickerLabel setTextColor:[UIColor BVTextDarkGrayColor]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}
#pragma mark - 监听pick的选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (0 == component) {
        BVAddressModel *model = self.days[row];
        _hours = model.cities;
        
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        
        if(_hours.count >0){
            BVAddressModel *model1 = _hours[0];
            _minutes = model1.cities;
        }else{
            _minutes = [NSMutableArray array];
        }
        [pickerView reloadComponent:2];
        
    }else if(1 == component){
        BVAddressModel *model = _hours[row];
        _minutes = model.cities;
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
    }
}
#pragma mark - 弹出
-(void)showMKPAlertView
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
}

-(void)creatShowAnimation
{
    self.alertView.layer.position = self.center;
    self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
        
    }];
}

-(void)disBtnClick:(UIButton *)btn{
    if (btn.tag == 100) {
        [self removeFromSuperview];
    }else{
        if (self.AlertResult) {
            self.AlertResult(_tempStr);
            [self removeFromSuperview];
        }
    }
}

- (void)fetchProfessionalData {
    
    [self.mineTool fetchProfessionalSuccess:^(NSMutableArray *days) {
        [self.days addObjectsFromArray:days];
        BVAddressModel *model = self.days[0];
        [self.hours addObjectsFromArray:model.cities];
        BVAddressModel *model2 = self.hours[0];
        [self.minutes addObjectsFromArray:model2.cities];
    } failure:^(NSError *error) {
        
    }];
    
}

-(UIButton *)upBtnWithTitle:(NSString *)title withTag:(NSInteger )tag{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.alertView addSubview:btn];
    [btn addTarget:self action:@selector(disBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [btn setTitleColor:[UIColor BVBaseOrangeColor]forState:UIControlStateNormal];
    [btn.layer setCornerRadius:3.0];
    [btn.layer setMasksToBounds:YES];
    [btn.layer setBorderWidth:1.0];
    [btn.layer setBorderColor:[UIColor BVLineGrayColor].CGColor];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTag:tag];
    
    return btn;
}

-(NSMutableArray *)days{
    if (!_days) {
        _days = [[NSMutableArray alloc]init];
    }
    return _days;
}
-(NSMutableArray *)hours{
    if (!_hours) {
        _hours = [[NSMutableArray alloc]init];
    }
    return _hours;
}
-(NSMutableArray *)minutes{
    if (!_minutes) {
        _minutes = [[NSMutableArray alloc]init];
    }
    return _minutes;
}

-(BVAllAlertTool *)mineTool{
    if (!_mineTool) {
        _mineTool = [[BVAllAlertTool alloc]init];
    }
    return _mineTool;
}


@end
